﻿@page "/"
@*<Test01 OnConfirms="@OnConfirm" />*@

<input type="@x" value="123456"/>

<button @onclick="xx" > 点 </button>
@code{
    string x= "password";

    void xx() { x = x == "password"?"text": "password"; }

    private Task OnConfirm(IEnumerable<Spec> param)
    {
        System.Console.WriteLine("选择" + param.Count().ToString());
        return Task.CompletedTask;
    }

}


<Search IgnoreCase="true" IsLikeMatch="true" IsAutoFocus="true" PlaceHolder="搜索示例" Items="@Items" OnSearch="@OnSearch1"></Search>


@code {
	private IEnumerable<string> Items => new string[] { "1", "12", "123", "1234" };

	private Task OnSearch1(string searchText)
	{
		System.Console.WriteLine($"SearchText: {searchText}");
		return Task.CompletedTask;
	}
}

@*<Tab>
    <TabItem Text="选择">
        <Test01 />
    </TabItem>
    <TabItem Text="选择">
        <Tab>
            <TabItem Text="选择">
                <Test01 />
            </TabItem>
            <TabItem Text="选择">
                <Test02 OnConfirms="@OnConfirm" />
            </TabItem>
            <TabItem Text="选择">
                <Test02 OnConfirms="@OnConfirm" />
            </TabItem>

        </Tab>
    </TabItem>
    <TabItem Text="选择">
        <Test01 OnConfirms="@OnConfirm" />
    </TabItem>

</Tab>*@

<Tab @ref="TabSetMenu" OnClickTab="@OnClickTabItem" />


@code{

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender) return;

        await base.OnAfterRenderAsync(firstRender);

        if (TabSetMenu==null) return;
        var flag = false;

        foreach (var item in   Pages )
        {
            TabSetMenu.AddTab(new Dictionary<string, object>
            {
                [nameof(TabItem.Text)] = item.Key,
                //[nameof(TabItem.ChildContent)] =  item.Value.Render()
                [nameof(TabItem.ChildContent)] = flag ? null : item.Value.Render()
                //[nameof(TabItem.ChildContent)] = new Lazy< RenderFragment>(()=> item.Value.Render())
            });
            flag = true;
        }

        TabSetMenu.ActiveTab(TabSetMenu.Items.FirstOrDefault());
    }

    private Task OnClickTabItem(TabItem item)
    {
        if (item.ChildContent == null)
        {
            item.ChildContent = (Pages.Where(a => a.Key == item.Text).FirstOrDefault().Value).Render();
        }
        return Task.CompletedTask;
    }


    private Tab TabSetMenu { get; set; }
    private Menu TabMenu { get; set; }

    private Dictionary<string, BootstrapDynamicComponent> Pages =
        new Dictionary<string, BootstrapDynamicComponent>
        {
            { "Test01",BootstrapDynamicComponent.CreateComponent<Test01>() },
            { "Test02",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            { "Test04",BootstrapDynamicComponent.CreateComponent<Test04>() } ,
            { "Test05",BootstrapDynamicComponent.CreateComponent<Test05>() } ,
            { "Test03",BootstrapDynamicComponent.CreateComponent<Test03>() } ,
            //{ "CCC1",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC2",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC3",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC4",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC5",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC6",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC7",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC8",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC9",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC11",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC12",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC13",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC14",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC15",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC16",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC17",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC18",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC19",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC20",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC21",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC22",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC23",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC24",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC25",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC26",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "CCC27",BootstrapDynamicComponent.CreateComponent<Test02>() } ,
            //{ "DDD",BootstrapDynamicComponent.CreateComponent<Test02>() }
        };

    private IEnumerable<MenuItem> GetSideMenuItems()
    {
        var menus = new List<MenuItem>();
        foreach (var item in Pages)
        {
            menus.Add(new MenuItem() { Text = item.Key });
        }
        return menus;
    }
    private Task OnClickMenuItem(MenuItem item)
    {
        var text = item.Text;
        var tabItem = TabSetMenu.Items.FirstOrDefault(i => i.Text == text);
        if (tabItem == null) AddTabItem(text ?? "");
        else TabSetMenu.ActiveTab(tabItem);
        return Task.CompletedTask;
    }

    private void AddTabItem(string text) => TabSetMenu.AddTab(new Dictionary<string, object>
    {
        [nameof(TabItem.Text)] = text,
        [nameof(TabItem.IsActive)] = true,
        [nameof(TabItem.ChildContent)] = (Pages.Where(a => a.Key == text).FirstOrDefault().Value).Render()
    });

    //private void AddTabItemChildContent(TabItem item) => item.ChildContent = (Pages.Where(a => a.Key == item.Text).FirstOrDefault().Value).Render();

}
